package com.shop.cloud.controller.system;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.shop.cloud.common.TreeNode;
import com.shop.cloud.common.mapper.JsonMapper;
import com.shop.cloud.common.mybatis.BaseController;
import com.shop.cloud.domain.system.SysResource;
import com.shop.cloud.filter.CurrentUserId;
import com.shop.cloud.filter.MagicPermission;
import com.shop.cloud.service.system.ISysResourceService;
import com.shop.cloud.util.DateUtils;
import com.shop.cloud.util.ErrorCode;
import com.shop.cloud.util.Response;
import com.shop.cloud.util.ResponseUtil;

@RestController
@RequestMapping(value = "resource")
public class SysResourceController extends BaseController {

	private static final Logger logger = Logger
			.getLogger(SysResourceController.class);

	@Autowired
	private ISysResourceService sysResourceService;

	@MagicPermission
	@RequestMapping(value = "find")
	@ResponseBody
	public Response find(String parentId,HttpServletRequest request) {
		Map<String, Object> params = getParamMap(request);
		if (StringUtils.isEmpty(parentId) || parentId.equals("#")) {
			params.put("parentId", null);
		}
		params.put("orderBy", "sort_no");
		List<SysResource> departments = sysResourceService.find(params);
		List<SysResource>  sysResourceService=TreeNode.tree(departments);
		return ResponseUtil.ok(sysResourceService);
	}

	@MagicPermission
	@RequestMapping(value = "findRoleResource", method = RequestMethod.POST)
	@ResponseBody
	public Response findRoleResource(Long roleId,HttpServletRequest request) {
		Map<String, Object> paramsMap=getParamMap(request);
		if("#".equals(paramsMap.get("parentId"))){
			paramsMap.remove("parentId");
		}
		paramsMap.put("status", 0);
		Map<String, Object> dataMap=new HashMap<String, Object>();
		List<SysResource> datas =sysResourceService.find(paramsMap);
		List<String> resourceIds=new ArrayList<String>();
		if(roleId!=null){
			resourceIds=sysResourceService.findRoleResource(roleId);
		}
		TreeNode root = new TreeNode();
		HashMap<Long, TreeNode> nodeList = new HashMap<Long, TreeNode>();
		for (Iterator<SysResource> it = datas.iterator(); it.hasNext();) {
			SysResource entity = it.next();
			TreeNode node = new TreeNode();
			node.id = entity.getId();
			node.text = entity.getName();
			node.parentId = entity.getParentId();
			nodeList.put(node.id, node);
		}
		Set<Map.Entry<Long, TreeNode>> entrySet = nodeList.entrySet();
		for (Iterator<Map.Entry<Long, TreeNode>> iterator = entrySet.iterator(); iterator.hasNext();) {
			Map.Entry<Long, TreeNode> entry = iterator.next();
			TreeNode node = entry.getValue();
			if (node.parentId==null) {
				root.addChild(node);
			} else {
				if(node.parentId!=null && node!=null){
					resourceIds.remove(node.parentId);
				}
				System.out.println(JsonMapper.nonDefaultMapper().toJson(node));
				nodeList.get(node.parentId).addChild(node);
			}
		}
		dataMap.put("checkData", resourceIds);
		dataMap.put("treeData", root.children);
		root.sortChildren(TreeNode.SysResourceComparatorEnum.ID);
		return ResponseUtil.ok(dataMap);
	}

	@MagicPermission
	@RequestMapping(value = "list")
	@ResponseBody
	public Response list(HttpServletRequest request) {
		Map<String, Object> params = getPageMap(request);
		return ResponseUtil.ok(sysResourceService.find(params));
	}

	@RequestMapping(value = "list2")
	@ResponseBody
	public Response list2(HttpServletRequest request) {
		Map<String, Object> params = getPageMap(request);
		return ResponseUtil.ok(sysResourceService.list(params));
	}
	
	@MagicPermission
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public Response update(@CurrentUserId Long userId,HttpServletRequest request, SysResource sysResource) {
		Date date = DateUtils.getSysDate();
		sysResource.setUpdateDate(date);
		sysResource.setUpdateUser(userId);	
		sysResourceService.update(sysResource);
		return ResponseUtil.ok();
	}

	@MagicPermission
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@ResponseBody
	public Response add(@CurrentUserId Long userId,HttpServletRequest request, SysResource sysResource,
			String roleIds) {
		Date date = DateUtils.getSysDate();
		sysResource.setCreateDate(date);
		sysResource.setUpdateDate(date);
		sysResource.setUpdateUser(userId);
		sysResource.setCreateUser(userId);
		sysResourceService.insert(sysResource, roleIds);
		return ResponseUtil.ok();
	}

	@MagicPermission
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	public Response delete(SysResource sysResource) {
		if(sysResource.getId()==null){
			return ResponseUtil.error(ErrorCode.PARAMETER_INVALID);
		}
		sysResource.setStatus(1);
		sysResourceService.deleteResource(sysResource);
		return ResponseUtil.ok();
	}

}